package com.example.beikeyigou.goods;

import android.content.Intent;
import android.os.Bundle;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.beikeyigou.R;
import com.example.beikeyigou.untils.BaseFragment;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


public class GoodsTypeFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private List<GoodsBean.DataBean> data;
    private DetailsBean detailsBean;
    private GoodsTypeAdapter adapter;
    public GoodsTypeFragment() {

    }


    // TODO: Rename and change types and number of parameters
    public static GoodsTypeFragment newInstance(String param1, String param2) {
        GoodsTypeFragment fragment = new GoodsTypeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_type, container, false);
        ListView lv = view.findViewById(R.id.frag_goodstype_lv);
        //数据源
        data = new ArrayList<>();

        adapter = new GoodsTypeAdapter(getContext(), data);
        lv.setAdapter(adapter);

        getNetWork(URLUtils.queryCommodityByCateId_url + mParam1);
        lv.setOnItemClickListener(listener);

        return view;
    }

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            GoodsBean.DataBean dataBean = data.get(position);

            RequestParams params = new RequestParams(URLUtils.queryCommodityInfo_url+dataBean.getId());
            x.http().get(params, new CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    //GoodsTypeFragment.super.onSuccess(result);
                    detailsBean = new Gson().fromJson(result, DetailsBean.class);
                    Intent intent = new Intent(getContext(), GoodsDetailsActivity.class);
                    intent.putExtra("goods",detailsBean.getData());
                    startActivity(intent);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }

    };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        GoodsBean goodsBean = new Gson().fromJson(result, GoodsBean.class);
        List<GoodsBean.DataBean> list = goodsBean.getData();
        data.addAll(list);
        adapter.notifyDataSetChanged();
    }
}